package com.example.node.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.node.R;
import com.example.node.base.BaseActivity;
import com.example.node.entity.Constant;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.MusicMoreModel;
import com.example.node.utils.GlideUtils;
import com.example.node.utils.OkHttpUtils;
import com.example.node.utils.VersionUtils;
import com.google.gson.Gson;
import java.io.IOException;

public class MusicMoreActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_cover;
    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_lyric;
    private TextView tv_info;
    private TextView tv_charge_edt;
    private String title;
    private String id;
    private MusicMoreModel.DataBean dataBean;
    private TextView mMusicTitle;
    private ImageView Btn_play,Btn_stop;
    public MediaPlayer mediaPlayer;
    private boolean isResult=false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_more);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        id = intent.getStringExtra("id");
        if (!TextUtils.isEmpty(title)) {
            getSupportActionBar().setTitle(title);
        }

        if (!TextUtils.isEmpty(id)) {
            initMusicMore();
        }

        iv_cover = findViewById(R.id.iv_cover);
        iv_cover.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        tv_lyric = findViewById(R.id.tv_lyric);
        tv_info = findViewById(R.id.tv_info);
        tv_charge_edt = findViewById(R.id.tv_charge_edt);
        mMusicTitle = findViewById(R.id.mMusicTitle);
        mMusicTitle.setText(title);
        Btn_play = findViewById(R.id.Btn_play);
        Btn_play.setOnClickListener(this);
        Btn_stop=findViewById(R.id.Btn_stop);
        Btn_stop.setOnClickListener(this);
    }

    //初始化音乐详情接口
    private void initMusicMore() {
        OkHttpUtils.get(Constant.HTTP_MUSIC_MORE + id, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    parsingMusicMoreJson(json);
                }
            }
        });
    }

    //解析音乐详情的数据
    @SuppressLint("NewApi")
    private void parsingMusicMoreJson(String json) {
        MusicMoreModel musicMoreModel = new Gson().fromJson(json, MusicMoreModel.class);
        dataBean = musicMoreModel.getData();
        GlideUtils.loadImageView(this, musicMoreModel.getData().getCover(), iv_cover);
        tv_title.setText(musicMoreModel.getData().getStory_title());

        CharSequence charSequence;
        if (VersionUtils.isN()) {
            charSequence = Html.fromHtml(musicMoreModel.getData().getStory(), Html.FROM_HTML_MODE_LEGACY);
        } else {
            charSequence = Html.fromHtml(musicMoreModel.getData().getStory());
        }
        tv_content.setText(charSequence);

        tv_lyric.setText(musicMoreModel.getData().getLyric());
        tv_info.setText(musicMoreModel.getData().getInfo());
        tv_charge_edt.setText(musicMoreModel.getData().getCharge_edt());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cover:
                if (dataBean != null) {
                    Intent intent = new Intent(this, WebViewActivity.class);
                    intent.putExtra("title", dataBean.getStory_title());
                    intent.putExtra("url", dataBean.getWeb_url());
                    startActivity(intent);
                }
                break;

            case R.id.Btn_play:
                play(dataBean.getMusic_id());
                break;

            case R.id.Btn_stop:
                mediaPlayer.pause();
                Btn_play.setVisibility(View.VISIBLE);
                Btn_stop.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 播放音乐
     */
    protected void play(String url) {
        Uri uri= Uri.parse(url);
        mediaPlayer = new MediaPlayer();
        // 设置指定的流媒体地址
        try {
            mediaPlayer.setDataSource(MusicMoreActivity.this,uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 设置音频流的类型
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        // 通过异步的方式装载媒体资源
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
                Btn_play.setVisibility(View.GONE);
                Btn_stop.setVisibility(View.VISIBLE);
            }
        });
        // 设置循环播放
        mediaPlayer.setLooping(true);
    }

    @Override
    protected void onDestroy() {
        // 在activity结束的时候回收资源
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }
}
